﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Project;
using AMM.Model;
using AMM.Model.Project;

namespace AMM.Business.Project {
    public class MouldShiftProcessor : BaseProcessor {

        private int _pageSize;
        private MouldShiftRepository _mouldShiftRepository;

        public MouldShiftProcessor(int pageSize) {
            this._pageSize = pageSize;
            this._mouldShiftRepository = new MouldShiftRepository(pageSize);
        }

        public MouldShift FetchMouldShiftById(int mouldShiftId) {
            try {
                return this._mouldShiftRepository.FetchMouldShiftById(mouldShiftId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定调模单失败！");
            }
        }

        public List<MouldShiftViewModel> FetchMouldShiftViewModelList(MouldShiftSearchModel searchModel, int pageIndex = 1) {
            try {
                List<MouldShift> resultList = this._mouldShiftRepository.FetchMouldShiftList(searchModel, pageIndex);
                return resultList.Select(x => {
                    return new MouldShiftViewModel() {
                        Key = x.MouldShiftId,
                        Code = x.Code,
                        FromVendorCode = x.Vendor.Code,
                        FromVendorName = x.Vendor.Name,
                        ToVendorCode = x.Vendor1.Code,
                        ToVendorName = x.Vendor1.Name,
                        FromWho = x.FromWho,
                        ToWho = x.ToWho,
                        FromWhy = x.FromWhy,
                        MouldCode = x.Mould.Code,
                        ShiftDate = x.ShiftDate,
                        OrderStatus = MethodHelper.GetEnumsDescription(typeof(AuditStatus), x.OrderStatus),
                        AuditedBy = x.AuditedBy,
                        AuditedDatetime = x.AuditedDatetime,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime,
                        ModifiedBy = x.ModifiedBy,
                        ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取调模单列表失败！");
            }
        }

        public void Insert(MouldShift mouldShift) {
            try {
                mouldShift.MouldShiftId = Guid.NewGuid().GetHashCode();
                mouldShift.CreatedDatetime = DateTime.Now;
                mouldShift.OrderStatus = (int)AuditStatus.Waiting;
                this._mouldShiftRepository.Insert(mouldShift);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("插入新调模单失败！");
            }
        }

        public void Update(MouldShift mouldShift) {
            try {
                MouldShift oldMouldShift = this.FetchMouldShiftById(mouldShift.MouldShiftId);
                if (!oldMouldShift.RowVersion.SequenceEqual(mouldShift.RowVersion)) {
                    throw this.BuildException("更新调模单失败，数据不一致性错误！");
                }
                mouldShift.ModifiedDatetime = DateTime.Now;
                this._mouldShiftRepository.UpdateMouldShift(mouldShift);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("更新调模单失败！");
            }
        }

        public void Delete(MouldShift mouldShift) {
            try {
                this._mouldShiftRepository.Delete(mouldShift);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除调模单失败！");
            }
        }

        public void Audit(MouldShift mouldShift) {
            try {
                MouldShift oldMouldShift = this.FetchMouldShiftById(mouldShift.MouldShiftId);
                if (!oldMouldShift.RowVersion.SequenceEqual(mouldShift.RowVersion)) {
                    throw this.BuildException("审核调模单失败，数据不一致性错误！");
                }
                mouldShift.AuditedDatetime = DateTime.Now;
                this._mouldShiftRepository.UpdateMouldShift(mouldShift);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("审核调模单失败！");
            }
        }

        public void Delete(params int[] mouldShiftIds) {
            try {
                this._mouldShiftRepository.DeleteMultipleMouldShift(mouldShiftIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除调模单失败！");
            }
        }
    }
}
